﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;

namespace RooHui.Infrastructure.Entities.Repository
{
    [Table("P_ProjectHistory")]
    public class P_ProjectHistory : IEntity<P_ProjectHistory>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        public int ProjectId { get; set; }
        public int PreState { get; set; }
        public int NewStatus { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(256)]
        public string ChangeContent { get; set; }
        public DateTime CreateTime { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(20)]
        public string CreateUser { get; set; }

        public override P_ProjectHistory Find(string key)
        {
            int Id = Int32.Parse(key);
            return Find(c => c.ID == Id);
        }

        public override P_ProjectHistory Find(System.Linq.Expressions.Expression<Func<P_ProjectHistory, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.ProjectHistory.FirstOrDefault(where);
                if (item != null)
                {
                    this.ChangeContent = item.ChangeContent;
                    this.CreateTime = item.CreateTime;
                    this.CreateUser = item.CreateUser;
                    this.ID = item.ID;
                    this.NewStatus = item.NewStatus;
                    this.PreState = item.PreState;
                    this.ProjectId = item.ProjectId;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<P_ProjectHistory, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.ProjectHistory.Any(where);
            }
        }
    }
}